﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp.Domain.Entities.Auditing;

namespace Coway.Ordering.Orders
{
    /// <summary>
    /// 订单信息
    /// </summary>
    /// <remarks>
    /// 参考：https://github.com/dotnet-architecture/eShopOnContainers/blob/dev/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs
    /// </remarks>
    public class Order : AuditedAggregateRoot<Guid>
    {
        protected Order()
        {
            //_orderItems = new List<OrderItem>();
            //_isDraft = false;
        }

        public Order(
            string orderNo,
            string referralNo,
            string buyWay
        ) : base()
        {
            OrderNo = orderNo;
            ReferralNo = referralNo;
            BuyWay = buyWay;
            OrderItems = new List<OrderItem>();
        }

        /// <summary>
        /// 订单号
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// 推荐人号
        /// </summary>
        public string ReferralNo { get; set; }

        /// <summary>
        /// 购买方式：买断、租赁
        /// </summary>
        public string BuyWay { get; set; }

        public string SellerName { get; set; }

        public string SellerCode { get; set; }

        public string SellerPhone { get; set; }

        /// <summary>
        /// 购买人ID
        /// </summary>
        public virtual Guid BuyerId { get; set; }

        /// <summary>
        /// 安装信息ID
        /// </summary>
        public virtual Guid InstallationInfoId { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        public virtual List<OrderItem> OrderItems { get; protected set; }

        public void SetOrderItems(List<OrderItem> orderItems)
        {
            OrderItems = orderItems;
        }

        //// DDD Patterns comment
        //// Using a private collection field, better for DDD Aggregate's encapsulation
        //// so OrderItems cannot be added from "outside the AggregateRoot" directly to the collection,
        //// but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour.
        //private readonly List<OrderItem> _orderItems;
        //public IReadOnlyCollection<OrderItem> OrderItems => _orderItems;

        //// DDD Patterns comment
        //// This Order AggregateRoot's method "AddOrderitem()" should be the only way to add Items to the Order,
        //// so any behavior (discounts, etc.) and validations are controlled by the AggregateRoot 
        //// in order to maintain consistency between the whole Aggregate. 
        //public void AddOrderItem(int productId, string productName, decimal unitPrice, decimal discount, string pictureUrl, int units = 1)
        //{
        //    //var existingOrderForProduct = _orderItems.Where(o => o.ProductId == productId)
        //    //    .SingleOrDefault();

        //    //if (existingOrderForProduct != null)
        //    //{
        //    //    //if previous line exist modify it with higher discount  and units..
        //    //    if (discount > existingOrderForProduct.GetCurrentDiscount())
        //    //    {
        //    //        existingOrderForProduct.SetNewDiscount(discount);
        //    //    }

        //    //    existingOrderForProduct.AddUnits(units);
        //    //}
        //    //else
        //    //{
        //    //    //add validated new order item
        //    //    var orderItem = new OrderItem(productId, productName, unitPrice, discount, pictureUrl, units);
        //    //    _orderItems.Add(orderItem);
        //    //}
        //}

        
    }
}
